<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="default" @click="handleImport()">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <a-popconfirm
          title="是否确认全部生效？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleEnableAll()"
        >
          <a-button type="success" v-auth="'shop:cus:shopCusCx:edit'">
            <Icon icon="ant-design:check-circle-outlined" /> {{ t('全部生效') }}
          </a-button>
        </a-popconfirm>
        <a-popconfirm
          title="是否确认生效？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleEnable()"
        >
          <a-button type="success" v-auth="'shop:cus:shopCusCx:edit'">
            <Icon icon="ant-design:check-circle-outlined" /> {{ t('确认生效') }}
          </a-button>
        </a-popconfirm>
        <a-popconfirm
          title="是否取消确认？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleCancel()"
        >
          <a-button type="danger" v-auth="'shop:cus:shopCusCx:edit'">
            <Icon icon="ant-design:close-circle-outlined" /> {{ t('取消确认') }}
          </a-button>
        </a-popconfirm>
        <a-popconfirm
          title="是否确认删除？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleBatchDel()"
        >
          <a-button type="danger" v-auth="'shop:cus:shopCusCx:edit'">
            <Icon icon="ant-design:delete-outlined" /> {{ t('批量删除') }}
          </a-button>
        </a-popconfirm>
        <a-button type="danger" @click="handleOpenModel()" v-auth="'shop:cus:shopCusCx:edit'">
          <Icon icon="ant-design:delete-outlined" /> {{ t('导入ID删除') }}
        </a-button>
        <a-button type="primary" @click="handleForm({})" v-auth="'shop:cus:shopCusCxHx:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.compName }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleExportSuccess" />
    <FormImport @register="registerImportModal" @success="handleExportSuccess" />
    <BasicModal
      v-bind="$attrs"
      :title="t('根据导入ID删除')"
      @register="registerModal"
      @ok="handleDelByExpId"
      :minHeight="90"
      :width="500"
    >
      <BasicForm @register="registerForm" />
    </BasicModal>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopCusShopCusCxHxList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, nextTick } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl2 } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { BasicModal, useModal } from '/@/components/Modal';
  import {
    shopCusCxHxDelete,
    shopCusCxHxListData,
    shopCusCxHxConfirm,
    shopCusCxHxCancel,
    shopCusCxHxBatchDel,
    shopCusCxEnableAll,
    shopCusCxHxDelByExpId,
  } from '/@/api/shop/cus/shopCusCxHx';
  import { useDrawer } from '/@/components/Drawer';
  import { BasicForm, FormProps, FormSchema, useForm } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';

  const { t } = useI18n('shop.cus.shopCusCxHx');
  const { showMessage, showMessageModal } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('促销核销管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('导入ID'),
        field: 'expId',
        component: 'Input',
      },
      {
        label: t('公司名称'),
        field: 'compName',
        component: 'Select',
        componentProps: {
          dictType: 'u8_accid',
          allowClear: true,
        },
      },
      {
        label: t('费用名称'),
        field: 'costName',
        component: 'Input',
      },
      {
        label: t('支付方式'),
        field: 'payType',
        component: 'Select',
        componentProps: {
          dictType: 'btdm_pay_type',
          allowClear: true,
        },
      },
      {
        label: t('是否收票'),
        field: 'bsp',
        component: 'Select',
        componentProps: {
          dictType: 'sys_yes_no',
          allowClear: true,
        },
      },
      {
        label: t('发票类型'),
        field: 'fpType',
        component: 'Select',
        componentProps: {
          dictType: 'btdm_fp_type',
          allowClear: true,
        },
      },
      {
        label: t('客户编码'),
        field: 'cusCode',
        component: 'Input',
      },
      {
        label: t('客户名称'),
        field: 'basCus.name',
        component: 'Input',
      },
      {
        label: t('客户简称'),
        field: 'basCus.abbName',
        component: 'Input',
      },
      {
        label: t('客户级别'),
        field: 'cusLevel',
        component: 'Input',
      },
      {
        label: t('导入日期'),
        field: 'expDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
        },
      },
      {
        label: t('生效日期'),
        field: 'auditDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'sys_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 80,
      align: 'center',
      dictType: 'sys_status',
      fixed: 'left',
    },
    {
      title: t('导入ID'),
      dataIndex: 'expId',
      key: 'a.exp_id',
      sorter: true,
      width: 165,
      align: 'left',
      fixed: 'left',
    },
    {
      title: t('公司名称'),
      dataIndex: 'compName',
      key: 'a.comp_name',
      sorter: true,
      width: 230,
      align: 'left',
      dictType: 'u8_accid',
      //slot: 'firstColumn',
    },
    {
      title: t('客户编码'),
      dataIndex: 'cusCode',
      key: 'a.cus_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('客户名称'),
      dataIndex: 'basCus.name',
      key: 'cus.name',
      sorter: true,
      width: 280,
      align: 'left',
    },
    {
      title: t('客户简称'),
      dataIndex: 'basCus.abbName',
      key: 'cus.abb_name',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('客户级别'),
      dataIndex: 'cusLevel',
      key: 'a.cus_level',
      sorter: true,
      width: 130,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('费用名称'),
      dataIndex: 'costName',
      key: 'a.cost_name',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('支付方式'),
      dataIndex: 'payType',
      key: 'a.pay_type',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'btdm_pay_type',
    },
    {
      title: t('核销金额'),
      dataIndex: 'imoney',
      key: 'a.imoney',
      sorter: true,
      width: 100,
      align: 'right',
    },
    {
      title: t('是否收票'),
      dataIndex: 'bsp',
      key: 'a.bsp',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'sys_yes_no',
    },
    {
      title: t('发票类型'),
      dataIndex: 'fpType',
      key: 'a.fp_type',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'btdm_fp_type',
    },
    {
      title: t('备注信息'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('导入日期'),
      dataIndex: 'expDate',
      key: 'a.exp_date',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('生效日期'),
      dataIndex: 'auditDate',
      key: 'a.audit_date',
      sorter: true,
      width: 100,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑促销核销'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'shop:cus:shopCusCxHx:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除促销核销'),
        popConfirm: {
          title: t('是否确认删除促销核销'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'shop:cus:shopCusCxHx:edit',
      },
    ],
  };

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('导入ID'),
      field: 'expId',
      component: 'Input',
      componentProps: {
        placeholder: t('请输入导入ID'),
      },
      required: true,
    },
  ];

  const [registerForm, { validate, setFieldsValue }] = useForm({
    labelWidth: 90,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerModal, { openModal, closeModal }] = useModal();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: shopCusCxHxListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl2({
      url: ctxAdminPath + '/shop/cus/shopCusCxHx/exportData',
      params: getForm().getFieldsValue(),
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDelete(record: Recordable) {
    const res = await shopCusCxHxDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleOpenModel() {
    openModal(true, {});
    await nextTick();
    setFieldsValue([{ expId: '' }]);
  }

  // 根据导入ID删除
  async function handleDelByExpId() {
    const values = await validate();
    const res = await shopCusCxHxDelByExpId({ expId: values.expId });
    closeModal();
    showMessage(res.message);
    handleSuccess();
  }

  // 全部生效
  async function handleEnableAll() {
    const res = await shopCusCxEnableAll().finally(() => {
      reload();
    });
    showMessage(res.message);
    handleSuccess();
  }

  // 确认生效
  async function handleEnable() {
    if (getSelectRows().length) {
      var selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.id + ',';
      });
      const res = await shopCusCxHxConfirm({ selIds: selIds.value }).finally(() => {
        reload();
      });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请选择一行数据！！');
    }
  }

  // 取消生效
  async function handleCancel() {
    if (getSelectRows().length) {
      var selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.id + ',';
      });
      const res = await shopCusCxHxCancel({ selIds: selIds.value });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请选择一行数据！！');
    }
  }

  // 批量删除
  async function handleBatchDel() {
    if (getSelectRows().length) {
      var selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.id + ',';
      });
      const res = await shopCusCxHxBatchDel({ selIds: selIds.value });
      if (res.data === false) {
        showMessageModal({ content: res.message });
      } else {
        showMessage(res.message);
      }
      handleSuccess();
    } else {
      showMessage('请选择一行数据！！');
    }
  }

  async function handleExportSuccess(params) {
    if (params.isImport) {
      await getForm().setFieldsValue({
        expId: params.expId,
      });
    } else {
      await getForm().setFieldsValue({
        expId: '',
      });
    }
    reload();
  }

  function handleSuccess() {
    reload();
  }
</script>
